/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package CapadeDatos;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
import recursos.Configuracion;

/**
 *
 *
 */

//Tipica Clase de Conexion que usabamos en las Clases de Lino pero con Otros Parametros
public class ConexionBD {
//    Configuracion config = new Configuracion();
//    private static ConexionBD instance;
//    private String url = config.getPropiedad("servidor.url");
    
    
    //Conexion con base de datos local
    public static String bd = "fermica_basecatalina";
    public static String connectionCollation="latin1_spanish_ci";
    public static String user ="fermica_admin20";
    public static String pass ="admin";
    public static String host ="localhost";
    public  static ConexionBD instance;

    
    //Conexion con servidor en internet prueba
//        public static String bd = "fermica_basecatalina";
//    public static String connectionCollation="latin1_spanish_ci";
//    public static String user ="fermica_admin20";
//    public static String pass ="fermindelcastillo";
//    public static String host ="199.85.212.227";
//    public  static ConexionBD instance;

    // Conexion con servidor en una red local
//    public static String bd = "fermica_basecatalina";
//    public static String connectionCollation="latin1_spanish_ci";
//    public static String user ="fermica_admin20";
//    public static String pass ="admin";
//    public static String host ="192.168.1.167";
//    public  static ConexionBD instance;

    public static String url = "jdbc:mysql://"+host+"/"+bd+"?user="+user+"&password="+pass+"&connectionCollation="+connectionCollation;
    public static ConexionBD getInstance() {
        if(instance == null){     instance = new ConexionBD();}
        return instance;
    }
    public Connection getConnection() {
        Connection cn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            cn = DriverManager.getConnection(url);
        }catch(ClassNotFoundException ex){
             JOptionPane.showMessageDialog(null,ex);
        }catch(SQLException ex){
          JOptionPane.showMessageDialog(null,ex);
        }catch(Exception ex) {
              JOptionPane.showMessageDialog(null,ex);
        }
        return cn;
    }
    public void close(Connection cn){
        try {
            if(cn != null)   cn.close();
        }catch(SQLException ex){
             //Registrar errores (Logging)
            System.out.println(ex);
        }
    }
    public void close(Statement stm){
        try {
            if(stm != null)
                stm.close();
        }catch(SQLException ex){
             //Registrar errores (Logging)
            System.out.println(ex);
        }
    }
    public void close(ResultSet rs){
        try {
            if(rs != null)
                rs.close();
        }catch(SQLException ex){
            System.out.println(ex);
        }
    }
}

